﻿
Ext.namespace('FantastPlatform', 'FantastPlatform.UI');
FantastPlatform.UI.AutoComplateComboBox = Ext.extend(Ext.form.ComboBox, {
    initComponent: function() {
        FantastPlatform.UI.AutoComplateComboBox.superclass.initComponent.call(this);
    },
    onRender: function(ct, position) {
        FantastPlatform.UI.AutoComplateComboBox.superclass.onRender.call(this, ct, position);
        if (Ext.isGecko) {
            this.el.dom.setAttribute('autocomplete', 'on');
        }
    },
    onTypeAhead: function() {
        if (this.store.getCount() > 0) {
            var r = this.store.getAt(0);
            var nowValue = this.getRawValue();
            var newValue;

            if (new RegExp("^\\d+$").test(nowValue)) {
                newValue = r.data[this.valueField];
            }
            else {
                newValue = r.data[this.displayField];
            }

            var len = newValue.length;
            var selStart = this.getRawValue().length;
            if (selStart != len) {
                this.setRawValue(newValue);
                this.selectText(selStart, newValue.length);
            }
        }
    },
    doQuery: function(q, forceAll) {
        if (q === undefined || q === null) {
            q = '';
        }
        var qe = {
            query: q,
            forceAll: forceAll,
            combo: this,
            cancel: false
        };
        if (this.fireEvent('beforequery', qe) === false || qe.cancel) {
            return false;
        }
        q = qe.query;
        forceAll = qe.forceAll;
        if (forceAll === true || (q.length >= this.minChars)) {
            if (this.lastQuery !== q) {
                this.lastQuery = q;
                if (this.mode == 'local') {
                    this.selectedIndex = -1;
                    if (forceAll) {
                        this.store.clearFilter();
                    } else {
                        var nowValue = this.getRawValue();
                        if (new RegExp("^\\d+$").test(nowValue)) {
                            this.store.filter(this.valueField, q);
                        }
                        else {
                            this.store.filter(this.displayField, q);
                        }
                    }
                    this.onLoad();
                } else {
                    this.store.baseParams[this.queryParam] = q;
                    this.store.load({
                        params: this.getParams(q)
                    });
                    this.expand();
                }
            } else {
                this.selectedIndex = -1;
                this.onLoad();
            }
        }
    }

});